package codetop.microsoft.T34;

/**
 * @Author: 18362
 * @Create: 2022-08-14 14:39:05 星期日
 */
class Solution {
    public int[] searchRange(int[] nums, int target) {
        int lower = lowerBound(nums, target);
        if (lower < 0)
            return new int[] {-1, -1};
        int higher = upperBound(nums, target);
        return new int[]{lower, higher-1};
    }

    private int lowerBound(int[] nums, int target) {
        if (nums.length == 0)
            return -1;
        int left = 0, right = nums.length-1;
        while (left < right) {
            int mid = left + (right - left) / 2;
            if (nums[mid] >= target)
                right = mid;
            else
                left = mid + 1;
        }
        return nums[left] == target ? left : -1;
    }

    private int upperBound(int[] nums, int target) {
        int left = 0, right = nums.length;
        while (left < right) {
            int mid = left + (right - left) / 2;
            if (nums[mid] > target)
                right = mid;
            else
                left = mid + 1;
        }
        return left;
    }
}